package com.webapp.controllers;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.webapp.models.FormBean;

/**
 * Servlet implementation class AjaxController
 */
@WebServlet("/AjaxController")
public class AjaxController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AjaxController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doRequest(request, response);
	}
	
	public void doRequest(HttpServletRequest request, HttpServletResponse response) throws IOException{
		HttpSession session = request.getSession();
		FormBean bean = (FormBean)session.getAttribute("Bean");
		
		System.out.println("poo");
		
		String var = request.getParameter("var");
		String attr = request.getParameter("attr");
		
		System.out.println(var);
		System.out.println(attr);
		
		if(var == ""){
			response.getWriter().println("Blank Field!");
		}
		
		if(attr.equals("password")) {
			bean.setPassword(var);
		}
		
		if(attr.equals("email")) {
			if(var.equals("") || !var.contains("@"))
				response.getWriter().println("Invalid Email");
		}
		if(attr.equals("confirmpassword")) {
			if(!(bean.getPassword().equals(var)))
				response.getWriter().println("Password Doesn't Match");
		}
		
		if(attr.equals("bPhone")) {
			if(var.equals("") || var.length() < 13)
				response.getWriter().println("Invalid Phone Number");
		}
		
		if(attr.equals("postal")) {
			if (var.equals("") || var.length() < 5)
				response.getWriter().println("Invalid Postal Code");
		}
	
	}        
}
